﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class Organization : System.Web.UI.Page
    {
        Departments_Dao dao = new Departments_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Department dept = dao.GetRoot();
                if (dept == null)
                {
                    return;
                }

                TreeNode root = new TreeNode();
                root.Text = dept.Name;
                root.Value = dept.Id.ToString();
                root.PopulateOnDemand = true;
                root.NavigateUrl = string.Format("DepartmentEdit.aspx?id={0}", root.Value);
                root.ImageUrl = "../images/user/group.png";

                tvOrg.Nodes.Add(root);
            }
        }

        protected void tvOrg_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            if (IsCallback)
            {
                if (e.Node.ChildNodes.Count == 0)
                {
                    LoadChildNode(e.Node);
                    LoadUsers(e.Node);
                }
            }
        }

        private void LoadChildNode(TreeNode node)
        {
            List<Department> ds = dao.GetChildren(node.Value);
            foreach (Department dept in ds)
            {
                TreeNode child = new TreeNode();
                child.Text = dept.Name;
                child.Value = dept.Id.ToString();
                child.PopulateOnDemand = true;
                child.NavigateUrl = string.Format("DepartmentEdit.aspx?id={0}", child.Value);
                child.ImageUrl = "../images/user/group.png";

                node.ChildNodes.Add(child);
            }
        }

        private void LoadUsers(TreeNode node)
        {
            List<User> us = dao.GetUsers(node.Value);
            foreach (User u in us)
            {
                TreeNode child = new TreeNode();
                child.Text = u.Name;
                child.Value = u.Id.ToString();
                child.PopulateOnDemand = true;
                child.NavigateUrl = string.Format("UserEdit.aspx?id={0}", child.Value);
                child.ImageUrl = "../images/user/user.png";
                child.Expanded = true;
                node.ChildNodes.Add(child);
            }
        }

        protected void tvOrg_SelectedNodeChanged(object sender, EventArgs e)
        {

        }

        protected void toggle()
        {
            TreeNode node = tvOrg.SelectedNode;
            string id = node.Value;
            string url = node.ImageUrl;
            bool isDept = url.IndexOf("group.png") > 0;
            //if (isDept)
            //{
            //    hlEdit.Enabled = true;
            //    hlEdit.NavigateUrl = string.Format("DepartmentEdit.aspx?id={0}", id);
            //    hlAddDepartment.Enabled = true;
            //    hlAddDepartment.NavigateUrl = string.Format("DepartmentEdit.aspx?pid={0}", id);
            //    hlAddUser.Enabled = true;
            //    hlAddUser.NavigateUrl = string.Format("UserEdit.aspx?pid={0}", id);
            //}
            //else
            //{
            //    hlEdit.Enabled = true;
            //    hlEdit.NavigateUrl = string.Format("UserEdit.aspx?id={0}", id);
            //    hlAddDepartment.Enabled = false;
            //    hlAddDepartment.NavigateUrl = "#";
            //    hlAddUser.Enabled = false;
            //    hlAddUser.NavigateUrl = "#";
            //}
        }
    }
}